<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>

<body>
    <div style="text-align: center;">
        <h1 id="txt" style="text-align: center;">还剩
            <sapn id="y"> </sapn>:
            <sapn id="d"> </sapn>:
            <sapn id="h"> </sapn>:
            <sapn id="m"></sapn>:
            <sapn id="s"></sapn>
        </h1>
        <!-- <span id="time"></span> -->
    </div>
</body>
<script>

    var year_ele = document.getElementById("y")
    var day_ele = document.getElementById("d")
    var hour_ele = document.getElementById("h")
    var min_ele = document.getElementById("m")
    var second_ele = document.getElementById("s")
    var year = 1000 * 60 * 60 * 24 * 365
    var day = 1000 * 60 * 60 * 24
    var hour = 1000 * 60 * 60
    var min = 1000 * 60
    var second = 1000
    var time2 = new Date("2021-4-30 18:10:00")

    var ss = setInterval(newtime, 1000)
    newtime()
    function newtime() {

        var time1 = new Date();
        var difftime = parseInt(time2 - time1);

        var s_year = parseInt(difftime / year)
        var s_day = ("" + parseInt((difftime % year) / day)).padStart(2, "0")
        var s_hour = ("" + parseInt((difftime % day) / hour)).padStart(2, "0")
        var s_min = ("" + parseInt((difftime % hour) / min)).padStart(2, "0")
        var s_second = ("" + parseInt((difftime % min) / second)).padStart(2, "0")
        // var str = s_year + ":" + s_day + ":" + s_hour + ":" + s_second + ":" + s_second

        year_ele.innerText = s_year + "年"
        day_ele.innerText = s_day + "天"
        hour_ele.innerText = s_hour + "小时"
        min_ele.innerText = s_min + "分"
        second_ele.innerText = s_second + "秒"
    }

</script>

</html>